import { Lua } from './lua_generator';

// export const math_number = function() {
//   // Numeric value.
//   var code = (this.getFieldValue('NUM'));
//   // -4.abs() returns -4 in Dart due to strange order of operation choices.
//   // -4 is actually an operator and a number.  Reflect this in the order.
//   var order = code < 0 ?
//       Lua.ORDER_UNARY_PREFIX : Lua.ORDER_ATOMIC;
//   return [code, order];
// };

Lua.math = {};
Lua.addReservedWords("math,random,Number");
export const math_number = function () {
    // a = parseFloat(a.getFieldValue("NUM"));
    // var b;
    // Infinity == a ? (a = 'float("inf")', b = Lua.ORDER_FUNCTION_CALL) : -Infinity == a ? (a = '-float("inf")', b = Lua.ORDER_UNARY_SIGN) : b = 0 > a ? Lua.ORDER_UNARY_SIGN : Lua.ORDER_ATOMIC;
    // return [a, b]

    var code = this.getFieldValue('NUM');
    // -4.abs() returns -4 in Dart due to strange order of operation choices.
    // -4 is actually an operator and a number.  Reflect this in the order.
    var order = code < 0 ?
        Lua.ORDER_UNARY_PREFIX : Lua.ORDER_ATOMIC;
    return [code, order];
};

export const math_constant = function () {
    var name = this.getFieldValue('CONSTANT');
    var code = 'math.' + name;
    return [code, Lua.ORDER_ATOMIC];
};


export const math_bit = function () {
    var operator = this.getFieldValue('OP');
    var order = Lua.ORDER_ATOMIC;
    var argument0 = Lua.valueToCode(this, 'A', order) || '0';
    var argument1 = Lua.valueToCode(this, 'B', order) || '0';
    var code = '(' + argument0 + operator + argument1 + ')';
    return [code, order];
};


export const math_arithmetic = function (a) {
    var b = {
            ADD: [" + ", Lua.ORDER_ADDITIVE],
            MINUS: [" - ", Lua.ORDER_ADDITIVE],
            MULTIPLY: [" * ", Lua.ORDER_MULTIPLICATIVE],
            DIVIDE: [" / ", Lua.ORDER_MULTIPLICATIVE],
            QUYU: [' % ', Lua.ORDER_MULTIPLICATIVE],//增加取余操作
            ZHENGCHU: [' // ', Lua.ORDER_MULTIPLICATIVE],//增加整除操作
            POWER: [" ^ ", Lua.ORDER_EXPONENTIATION]
        }[a.getFieldValue("OP")],
        c = b[0],
        e = b[1],
        d = Lua.valueToCode(a, "A", e) || "0";
    a = Lua.valueToCode(a, "B", e) || "0";
    return [d + c + a, e];
};

export const math_single = function (a) {
    var b = a.getFieldValue("OP"),
        c;
    if ("NEG" == b)
        return c = Lua.valueToCode(a, "NUM", Lua.ORDER_UNARY_SIGN) || "0", ["-" + c, Lua.ORDER_UNARY_SIGN];
    a = "SIN" == b || "COS" == b || "TAN" == b ? Lua.valueToCode(a, "NUM", Lua.ORDER_MULTIPLICATIVE) || "0" : Lua.valueToCode(a, "NUM", Lua.ORDER_NONE) || "0";
    switch (b) {
    case "ABS":
        c = "math.abs(" + a + ")";
        break;
    case "ROOT":
        c = "math.sqrt(" +
                a + ")";
        break;
    case "LN":
        c = "math.log(" + a + ")";
        break;
    case "LOG10":
        c = "math.log(" + a + ", 10)";
        break;
    case "EXP":
        c = "math.exp(" + a + ")";
        break;
    case "ROUND":
        c = "round(" + a + ")";
        break;
    case "ROUNDUP":
        c = "math.ceil(" + a + ")";
        break;
    case "ROUNDDOWN":
        c = "math.floor(" + a + ")";
        break;
    case "SIN":
        c = "math.sin(math.rad(" + a + "))";
        break;
    case "COS":
        c = "math.cos(math.rad(" + a + "))";
        break;
    case "TAN":
        c = "math.tan(math.rad(" + a + "))";
        break;
    case "-":
        c = "-(" + a + ")";
        break;
    default:
    }
    if (c)
        return [c, Lua.ORDER_EXPONENTIATION];
    switch (b) {
    case "ASIN":
        c = "math.deg(math.asin(" + a + "))";
        break;
    case "ACOS":
        c = "math.deg(math.acos(" + a + "))";
        break;
    case "ATAN":
        c = "math.deg(math.atan(" + a + "))";
        break;
    }
    return [c, Lua.ORDER_MULTIPLICATIVE];
};


export const math_trig = math_single;

export const math_to_int = function () {
    var argument0 = Lua.valueToCode(this, 'A', Lua.ORDER_NONE) || '0';
    var operator = this.getFieldValue('OP');
    var code = "";
    if (operator === "round") {
        code = operator + '(' + argument0 + ')';
    } else {
        code = "math." + operator + '(' + argument0 + ')';
    }
    return [code, Lua.ORDER_ATOMIC];
};

export const math_max_min = function () {
    var a = Lua.valueToCode(this, 'A', Lua.ORDER_NONE) || '0';
    var b = Lua.valueToCode(this, 'B', Lua.ORDER_NONE) || '0';
    var operator = this.getFieldValue('OP');
    var code = operator + '(' + a + ', ' + b + ')';
    return [code, Lua.ORDER_ATOMIC];
};


export const math_random = function () {
    // Random integer between [X] and [Y].
    var type = this.getFieldValue('TYPE');
    var argument0 = Lua.valueToCode(this, 'FROM',
        Lua.ORDER_NONE) || '0';
    var argument1 = Lua.valueToCode(this, 'TO',
        Lua.ORDER_NONE) || '0';
    var code = '';
    if (type == 'int') {
        code = 'random.randint(' + argument0 + ', ' + argument1 + ')';
    } else if (type == 'float') {
        code = 'random.uniform(' + argument0 + ', ' + argument1 + ')';
    }
    return [code, Lua.ORDER_UNARY_POSTFIX];
};


export const math_map = function () {
    var value_num = Lua.valueToCode(this, 'NUM', Lua.ORDER_NONE);
    var value_fl = Lua.valueToCode(this, 'fromLow', Lua.ORDER_ATOMIC);
    var value_fh = Lua.valueToCode(this, 'fromHigh', Lua.ORDER_ATOMIC);
    var value_tl = Lua.valueToCode(this, 'toLow', Lua.ORDER_ATOMIC);
    var value_th = Lua.valueToCode(this, 'toHigh', Lua.ORDER_ATOMIC);
    var code = 'math_map(' + value_num + ', ' + value_fl + ', ' + value_fh + ', ' + value_tl + ', ' + value_th + ')';
    return [code, Lua.ORDER_NONE];
};

export const math_constrain = function () {
    // Constrain a number between two limits.
    var argument0 = Lua.valueToCode(this, 'VALUE',
        Lua.ORDER_NONE) || '0';
    var argument1 = Lua.valueToCode(this, 'LOW',
        Lua.ORDER_NONE) || '0';
    var argument2 = Lua.valueToCode(this, 'HIGH',
        Lua.ORDER_NONE) || '0';
    var code = 'min(max(' + argument0 + ', ' + argument1 + '), ' + argument2 + ')';
    return [code, Lua.ORDER_UNARY_POSTFIX];
};

export const math_random_seed = function () {
    // Random integer between [X] and [Y].
    var a = Lua.valueToCode(this, 'NUM', Lua.ORDER_NONE) || '0';
    var code = 'random.seed(' + a + ');' + '\n';
    return code;
};

export const math_indexer_number = function () {
    var code = this.getFieldValue('NUM');
    // -4.abs() returns -4 in Dart due to strange order of operation choices.
    // -4 is actually an operator and a number.  Reflect this in the order.
    var order = code < 0 ?
        Lua.ORDER_UNARY_PREFIX : Lua.ORDER_ATOMIC;
    return [code, order];
}

export const math_round = function () {
    var argument0 = Lua.valueToCode(this, 'VALUE',
        Lua.ORDER_NONE) || '0';
    var argument1 = Lua.valueToCode(this, 'VAR',
        Lua.ORDER_NONE) || '0';

    var code = 'round(' + argument0 + ', ' + argument1 + ')';
    return [code, Lua.ORDER_ATOMIC];
};

export const base_map = math_map